<template>
    <basic-container>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-setting"></i> 商家管理</el-breadcrumb-item>
                <el-breadcrumb-item><i class="el-icon-date"></i> 平台商家详情</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <avue-detail ref="form" v-model="form" :option="formOption" v-show="isEdit">
        </avue-detail>

        <avue-form ref="form" v-model="form" :option="formOption" v-show="!isEdit">
        </avue-form>

        <el-row type="flex" justify="center">
            <el-button type="primary" @click="handleInit" v-show="!isEdit" :loading="btnLoading">初始化</el-button>
            <el-button type="primary" @click="handleModify" v-show="isEdit" :loading="btnLoading">修 改</el-button>
            <el-button @click="goBack">返 回</el-button>
        </el-row>

    </basic-container>
</template>

<script>
    import {getPlatform, initPlatform} from '../../../api/manage/merchant';
    import {formOption} from '../../../const/crud/opc/form_option/merchant_edit';
    import {goBack} from "../../../util/util";

    export default {
        data() {
            return {
                form: {},
                formOption: formOption,
                merchantId: '',
                btnLoading: false,
                isEdit: true
            }
        },
        created() {
            this.handleDetail();
        },
        methods: {
            handleDetail() {
                getPlatform().then(response => {
                    this.form = response.data.data;
                }).catch(() => {
                    this.isEdit = false;
                    this.form.status = '2';
                    this.form.pdsType = '1';
                    this.$message.info("若无平台商家, 需初始化");
                });
            },
            handleModify() {
                let query = {
                    res: {
                        merchantId: this.form.merchantId
                    }
                };
                this.$store.commit('DEL_ONE_TAG', this.$route);
                this.$router.push({path: '/manage/merchant_edit', query: query})
            },
            handleInit() {
                this.btnLoading = true;
                this.$refs.form.validate(res => {
                    if (res) {
                        initPlatform(this.form).then(response => {
                            this.$message.success("初始化平台商家成功");
                            this.form = response.data.data;
                            this.isEdit = true;
                        })
                    }
                    this.btnLoading = false;
                });
            },
            goBack(time = 1000) {
                goBack(this, time);
            }
        }
    }
</script>

<style scoped lang="scss">
    .crumbs {
        margin: 20px 10px;
    }
</style>


